package com.cssl.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cssl.pojo.History;
import com.cssl.pojo.User;
import com.cssl.service.HistoryService;
import com.cssl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
public class HistoryController {

    @Autowired
    private HistoryService historyService;

    @Autowired
    private UserService userService;

    @PostMapping("/historyAll")
    public String historyAll(){
        List<History> list = historyService.list();
        String str= JSON.toJSONStringWithDateFormat(list,"yyyy-MM-dd");
        return str;
    }

    @PostMapping("/sousuo")
    public String sousuo(String date1,String date2,Integer uid){
        System.out.println(date1+".."+date2);
        if(date1!=""&&date2!=""&&uid!=0){
            System.out.println("11111");
            String str = JSON.toJSONStringWithDateFormat(historyService.list(new QueryWrapper<History>().between("time",date1,date2)
                    .and(historyQueryWrapper -> historyQueryWrapper.eq("uid",uid))),"yyyy-MM-dd");
            return str;
        }else if(date1!=""||date2!=""||uid!=0){
            System.out.println("222222");
            String str2 = JSON.toJSONStringWithDateFormat(historyService.list(new QueryWrapper<History>().between("time",date1,date2)
                    .or(historyQueryWrapper -> historyQueryWrapper.eq("uid",uid))),"yyyy-MM-dd");
            return str2;
        }
        System.out.println(3333);
        String str3 = JSON.toJSONStringWithDateFormat(historyService.list(),"yyyy-MM-dd");
        return str3;
    }

    @PostMapping("/addHistory")
    public boolean addHistory(History history){
        history.setTime(new Date());
        return historyService.save(history);
    }


}
